package com.wonders.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.wonders.demo.util.HttpUtils;
import com.wonders.demo.util.SignUtils;

import java.util.Map;
import java.util.TreeMap;

public class TestSign {
    public static void main(String[] args) {
        String getTokenUrl = "https://lcsmy.liuzhou.gov.cn/oauth2/app/token";
        String appPrivateKey="MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAL23LPjJoHakPMpg8rQo9NF5lLT7zypIPYnZ1paKgnzPwPbAH7tQgN9nKb+cNuGo4fg9jB9ncM0JpMPKnx4f43bV3XKYvwCWlQMbUI6cIG25zSYLFZ51x0tgUi4o3Bj0o2P7TfaYyhVv0lSA/XIngFeDVQB50snyAQ+y8TbhSbZhAgMBAAECgYAiM67I31itauiGt7mpnySbyyZzBl6PydxGSN5131ys8athELvnwhipreiJ4bjHL/IlFbt4QebFIDLzFUlgXfFcwGmc1T8eApI4sw9puQ1Ev8ue721A3ERRT10puN51KBNzRyOGHBh3D2yoRp9quFE9o1Mc4t0ffuvSFQ6OSd8BQQJBAPBDGcOVZM7d+VhMc9CPz2SFcnpIh9EikaKEC7c+kbE01eNR/qJ+p48guilZQ9XTit2mmMM0ELkU8dgRBKYb7pkCQQDKJHifKyQu2L8nqJYdpB+Zanu/28tcArTDy/zUHH+lh1AtX850pXQp5Gtche0E86GJ3aKruJu13weKTXqc08sJAkEAlGQNHJQ/j9jfMN7/RNgdcmtNWiND5zIfmq1Ngx7c4wLL+7tftsLdAHRpJjKgyAcApIdJoH6euYErIZTZNzLXOQJAQYpbtbfmLm5GZFRr/8ZUHIkDAK0CHZ4fABDms9tdo8zguSWEKhvvMxoFmWUu+VkI+ktrT+Q2UFy09Cm05J5jCQJBAO9FigTG2R4TOqztYTeL+mvkJDX0MZfFtYmy/+Tx4zr0i3PJ0iPMKvRrNsQCHlPlXwn4OsFouTlUoRR/d67j2T0=";
        String appPublicKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC9tyz4yaB2pDzKYPK0KPTReZS0+88qSD2J2daWioJ8z8D2wB+7UIDfZym/nDbhqOH4PYwfZ3DNCaTDyp8eH+N21d1ymL8AlpUDG1COnCBtuc0mCxWedcdLYFIuKNwY9KNj+032mMoVb9JUgP1yJ4BXg1UAedLJ8gEPsvE24Um2YQIDAQAB";

        Map<String, Object> params = new TreeMap<>();
        params.put("code", "CEathL");
        params.put("client_id", "c91eac32d1a64118b591061bcb467960");
        params.put("redirect_uri", "https://10.35.252.230");
        params.put("isEncrypt", 0);
        //拼接
        StringBuilder stringBuilder = new StringBuilder();
        for (String key : params.keySet()) {
            stringBuilder.append(key).append("=").append(params.get(key)).append("&");
        }
        //加签
        String content = stringBuilder.toString().substring(0, stringBuilder.length() - 1);
        System.out.println("加签内容："+content);
        //生产签名
        String sign = SignUtils.sign(content, appPrivateKey);
        System.out.println("生成签名："+sign);
        params.put("sign", sign);

        //验签
        System.out.println("验签结果："+SignUtils.verify(content,sign,appPublicKey));



        //拿code换区token
        try {
//            JSONObject result = (JSONObject) HttpUtils.syncPost(getTokenUrl, params, null);
//            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
